/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: exec_init.cpp
 *
 * Purpose: xgit initialize executor implementation 
 *
 * Developer:
 *   wen.gu , 2024-01-19
 *
 * TODO:
 *
 ***************************************************************************/
#include "xgit/exec_init.h"
#include "arg_parser.h"

namespace xgit {

struct InitParam { 
    std::string url;
    std::string revision;
    std::string manifest_name;
};

class ExecInit::Impl {
public:
    icpp::core::ArgParser arg_parser_;
    InitParam param_;
};

ExecInit::ExecInit()
    :impl_(new Impl){
    //todo something
    icpp::core::ArgParser& arg_parser = impl_->arg_parser_;
    arg_parser.addWithParamNoneHandler("help",'?', "show this usage info", [&arg_parser]() -> icpp::core::ArgumentParseError  {
        arg_parser.showHelp();
        return icpp::core::ArgumentParseError::kDoCommand; /** the help info need return a specail error code.*/
    });
    arg_parser.addWithParamRequired<std::string>("url", 'u', "xgit repository url", true, impl_->param_.url);
    arg_parser.addWithParamRequired<std::string>("branch", 'b', "the revision of xgit repository", false, impl_->param_.revision, "master");
    arg_parser.addWithParamRequired<std::string>("name", 'n', "The manifest file name", false, impl_->param_.manifest_name, "default.yaml");
}

ExecInit::~ExecInit(){

}



bool ExecInit::exec(int argc, const char* const argv[]) {
    icpp::core::ArgParser& arg_parser = impl_->arg_parser_;
    icpp::core::ArgumentParseError ret = arg_parser.onParse(argc, argv);

    if (icpp::core::ArgumentParseError::kSuccess == ret) {
        std::cout << "got url: " << impl_->param_.url << ", branch: " << impl_->param_.revision << ", manifest file name: " << impl_->param_.manifest_name << std::endl;

    } else  {
        if (ret != icpp::core::ArgumentParseError::kDoCommand) {
            std::cout << "error: " << arg_parser.error() << std::endl;
            arg_parser.showHelp();
        }
    }    
    return true;

}

}